package com.glodon.ggzy.user.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.glodon.ggzy.core.model.Page;
import com.glodon.ggzy.core.utils.PageSearchModel;
import com.glodon.ggzy.core.utils.QuerySupport;
import com.glodon.ggzy.entity.JgMenu;
import com.glodon.ggzy.entity.JgRole;
import com.glodon.ggzy.entity.JgRoleMenuRelation;
import com.glodon.ggzy.entity.JgUser;
import com.glodon.ggzy.user.dao.IJgRoleMenuRelationDao;
import com.glodon.ggzy.user.dao.IJgptMeunDao;
import com.glodon.ggzy.user.dao.IJgptRoleDao;
import com.glodon.ggzy.user.dao.IJgptUserDao;
import com.glodon.ggzy.user.service.IJgptUserService;
import com.glodon.ggzy.vo.AuthorityMenu;

@Service
@Transactional
public class JgptUserServiceImpl implements IJgptUserService {

	@Autowired
	private IJgptUserDao jgptUserDao;

	@Autowired
	private IJgptRoleDao roleDao;

	@Autowired
	private IJgptMeunDao meunDao;

	@Autowired
	private IJgRoleMenuRelationDao roleMenuRelationDao;

	@Override
	public boolean saveJgptMenu(JgMenu menu) {
		if (menu == null) {
			return false;
		}
		meunDao.save(menu);
		return true;
	}

	@Override
	public Page queryJgptMenu(PageSearchModel queryModel, String name) {
		queryModel.addParamToMap(QuerySupport.QUERYCLASS, JgMenu.class);
		if (!StringUtils.isEmpty(name)) {
			queryModel.addParamToMap("meunName_like", "%" + name + "%");
		}
		return meunDao.pageQuery(queryModel.getParamsMap(), queryModel.getPageNo(), queryModel.getPageSize());
	}

	@Override
	public boolean checkUser(String userName, String password) {
		List<Object> find = jgptUserDao.find("from JgUser where userName = ? and password = ?", userName, password);
		if (find.size() > 0) {
			return Boolean.TRUE;
		}
		return Boolean.FALSE;
	}

	@Override
	public JgUser findUser(String userName, String password) {
		List<Object> find = jgptUserDao.find("from JgUser where userName = ? and password = ?", userName, password);
		if (find.size() > 0) {
			return (JgUser) find.get(0);
		}
		return null;
	}

	@Override
	public List<AuthorityMenu> queryAuthorityByUserId(String user_Id) {
		StringBuffer hql = new StringBuffer();
		hql.append(" select user,menu ");
		hql.append(" from  JgUser user, JgMenu menu,JgRoleMenuRelation relation");
		hql.append(" where  user.roleId =  relation.roleId and  menu.id = relation.menuId");
		hql.append("        and user.id=?");
		List<Object> find = jgptUserDao.find(hql.toString(), user_Id);
		List<AuthorityMenu> result = new ArrayList<AuthorityMenu>();
		for (int i = 0; i < find.size(); i++) {
			Object[] object = (Object[]) find.get(i);
			JgUser user = (JgUser) object[0];
			JgMenu menu = (JgMenu) object[1];

			String userId = user.getId();
			String roleId = user.getRoleId();
			String menuId = menu.getId();
			String meunName = menu.getMenuName();
			String menuUrl = menu.getMenuUrl();

			AuthorityMenu authMenu = new AuthorityMenu();
			authMenu.setUserId(userId);
			authMenu.setRoleId(roleId);
			authMenu.setMenuId(menuId);
			authMenu.setMenuName(meunName);

			authMenu.setMenuUrl(menuUrl + "?menuId=" + menuId);

			result.add(authMenu);
		}
		return result;
	}

	@Override
	public Page queryJgptRole(PageSearchModel queryModel, String name) {
		queryModel.addParamToMap(QuerySupport.QUERYCLASS, JgRole.class);
		if (!StringUtils.isEmpty(name)) {
			queryModel.addParamToMap("roleName_like", "%" + name + "%");
		}
		return roleDao.pageQuery(queryModel.getParamsMap(), queryModel.getPageNo(), queryModel.getPageSize());
	}

	@Override
	public boolean saveJgptRole(JgRole role, String menuIds) {
		if (role == null) {
			return false;
		}
		//先保存角色
		roleDao.save(role);
		//先删除角色和菜单关联表
		delRoleMenuRelationByRoleId(role.getId());
		//保存角色和菜单表关联表
		if (!StringUtils.isEmpty(menuIds)) {
			String muids[] = menuIds.split(",");
			for (String menuId : muids) {
				JgRoleMenuRelation relation = new JgRoleMenuRelation();
				relation.setRoleId(role.getId());
				relation.setMenuId(menuId);
				roleMenuRelationDao.save(relation);
			}
		}
		return true;
	}

	private boolean delRoleMenuRelationByRoleId(String roleId) {
		if (StringUtils.isEmpty(roleId)) {
			return false;
		}
		List<JgRoleMenuRelation> relationList = new ArrayList<JgRoleMenuRelation>();
		DetachedCriteria criteria = DetachedCriteria.forClass(JgRoleMenuRelation.class);
		criteria.add(Restrictions.eq("roleId", roleId));
		relationList = roleMenuRelationDao.findByDetachedCriteria(criteria);
		if (relationList != null && relationList.size() > 0) {
			for (JgRoleMenuRelation relation : relationList) {
				roleMenuRelationDao.delete(relation);
			}
		}
		return true;
	}

	@Override
	public Page queryJgptUser(PageSearchModel queryModel, String name) {
		queryModel.addParamToMap(QuerySupport.QUERYCLASS, JgUser.class);
		if (!StringUtils.isEmpty(name)) {
			queryModel.addParamToMap("userName_like", "%" + name + "%");
		}
		return jgptUserDao.pageQuery(queryModel.getParamsMap(), queryModel.getPageNo(), queryModel.getPageSize());
	}

	@Override
	public boolean saveJgptUser(JgUser user) {
		if (user == null) {
			return false;
		}
		user.setCreateTime(new Date());
		jgptUserDao.save(user);
		return true;
	}

	@Override
	public List<JgMenu> getJgMenuAll() {
		List<JgMenu> menuList = new ArrayList<JgMenu>();
		DetachedCriteria criteria = DetachedCriteria.forClass(JgMenu.class);
		menuList = meunDao.findByDetachedCriteria(criteria);
		return menuList;
	}

	@Override
	public String getMeunNameByRoleId(String roleId) {
		String menuName = "";
		List<JgRoleMenuRelation> relationList = new ArrayList<JgRoleMenuRelation>();
		DetachedCriteria criteria = DetachedCriteria.forClass(JgRoleMenuRelation.class);
		criteria.add(Restrictions.eq("roleId", roleId));
		relationList = roleMenuRelationDao.findByDetachedCriteria(criteria);
		if (relationList != null && relationList.size() > 0) {
			for (JgRoleMenuRelation relation : relationList) {
				JgMenu menu = getJgMenuById(relation.getMenuId());
				if (StringUtils.isEmpty(menuName)) {
					menuName = menu.getMenuName();
				} else {
					menuName += "," + menu.getMenuName();
				}
			}
		}
		return menuName;
	}

	@Override
	public JgMenu getJgMenuById(String id) {
		return meunDao.get(id);
	}

	@Override
	public JgRole getJgRoleById(String id) {
		return roleDao.get(id);
	}

	@Override
	public List<JgRole> getJgRoleAll() {
		List<JgRole> roleList = new ArrayList<JgRole>();
		DetachedCriteria criteria = DetachedCriteria.forClass(JgRole.class);
		roleList = roleDao.findByDetachedCriteria(criteria);
		return roleList;
	}

	@Override
	public JgUser isUserNameExist(String userName) {
		if (StringUtils.isEmpty(userName)) {
			return null;
		}
		List<JgUser> userList = new ArrayList<JgUser>();
		DetachedCriteria criteria = DetachedCriteria.forClass(JgUser.class);
		criteria.add(Restrictions.eq("userName", userName));
		userList = jgptUserDao.findByDetachedCriteria(criteria);
		if (userList != null && userList.size() > 0) {
			return userList.get(0);
		}
		return null;
	}

	@Override
	public void deletJgUserById(String id) {
		jgptUserDao.deleteById(id);
	}

	@Override
	public JgUser getJgUserById(String id) {
		return jgptUserDao.get(id);
	}

	@Override
	public String getCheckedMenuId(String roleId, String menuId) {
		String checkMenuId = "";
		List<JgRoleMenuRelation> relationList = new ArrayList<JgRoleMenuRelation>();
		DetachedCriteria criteria = DetachedCriteria.forClass(JgRoleMenuRelation.class);
		criteria.add(Restrictions.eq("roleId", roleId));
		criteria.add(Restrictions.eq("menuId", menuId));
		relationList = roleMenuRelationDao.findByDetachedCriteria(criteria);
		if (relationList != null && relationList.size() > 0) {
			JgRoleMenuRelation relation = relationList.get(0);
			checkMenuId = relation.getMenuId();
		}
		return checkMenuId;
	}

	@Override
	public void deletJgRoleById(String id) {
		roleDao.deleteById(id);
	}

	@Override
	public void deletJgMenuById(String id) {
		meunDao.deleteById(id);
	}
}
